package net.wanho.personalOffice.dao.impl;

import net.wanho.commons.dao.impl.BaseDAOImpl;
import net.wanho.commons.exception.DAOException;
import net.wanho.commons.exception.DataAccessException;
import net.wanho.personalOffice.dao.CommentDAO;
import net.wanho.personalOffice.po.Comment;
import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class CommentDAOImpl extends BaseDAOImpl<Comment> implements CommentDAO {

    @Override
    public boolean insert(Comment entity) throws DAOException {
        try {
            String sql ="INSERT INTO CRM.comment (\n" +
                    "  RECORD_ID,\n" +
                    "  COMMENT_EMPLOYEE,\n" +
                    "  DETAIL,\n" +
                    "  CREATE_TIME\n" +
                    ")\n" +
                    "VALUES\n" +
                    "  (?, ?, ?, ?);";
            Object[] params = {
                    entity.getRecordId(),
                    entity.getCommentEmployee().getEmployeeId(),
                    entity.getDetail(),
                    new Timestamp(new Date().getTime())
            };
            return super.jdbcTemplate.update(sql,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public List<Comment> selectByCond(Map<String, String> condMap) throws DAOException {
        try {
            String sql ="SELECT\n" +
                    "  c.COMMENT_ID commentId,\n" +
                    "  c.RECORD_ID recordId,\n" +
                    "  c.DETAIL detail,\n" +
                    "  c.CREATE_TIME createTime,\n" +
                    "  e.EMPLOYEE_ID \"commentEmployee.employeeId\",\n" +
                    "  e.EMPLOYEE_NAME \"commentEmployee.employeeName\",\n" +
                    "  e.DEPARTMENT_ID \"commentEmployee.departmentId\",\n" +
                    "  e.POSITON_ID \"commentEmployee.positionId\",\n" +
                    "  e.PHONE \"commentEmployee.phone\",\n" +
                    "  e.EMAIL \"commentEmployee.email\",\n" +
                    "  e.STATUS \"commentEmployee.status\",\n" +
                    "  e.PARENT_EMPLOYEE_ID \"commentEmployee.parentEmployeeId\",\n" +
                    "  e.CREATE_TIME \"commentEmployee.createTime\",\n" +
                    "  e.UPDATE_TIME \"commentEmployee.updateTime\"\n" +
                    "FROM CRM.comment c\n" +
                    "LEFT JOIN employee e\n" +
                    "ON c.COMMENT_EMPLOYEE = e.EMPLOYEE_ID\n" +
                    "WHERE 1=1";
            StringBuffer sb = dySQL(condMap,sql);
            Object[] params = {};
            return super.queryForList(sb.toString(),params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public Long selectCountByRecord(Integer id) throws DAOException {
        try {
            String sql ="SELECT\n" +
                    "  COUNT(0)\n" +
                    "FROM `CRM`.`comment` c\n" +
                    "WHERE c.`RECORD_ID`=?";
            Object[] params = {id};
            return super.jdbcTemplate.queryForLong(sql,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }
    private StringBuffer dySQL(Map<String, String> condMap, String sql) {
        StringBuffer sb = new StringBuffer(sql) ;
        String recordId = condMap.get("recordId");
        //根据类别id 精确查询
        if (StringUtils.isNotEmpty(recordId)){
            sb.append(" AND c.RECORD_ID=").append(Integer.parseInt(recordId)) ;
        }
        return  sb;
    }

}
